feature

Several methods. They are called as Rest Architecture. What is REST architecture? REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol. A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol. It usually done using json fromat

1)GET Method - This is used to provide a read only access to a resource.

2)PUT Method - This is used to create a new resource.

3)POST Method - This is used to update a existing resource or create a new resource.

4)DELETE Method - This is used to remove a resource. Get Method :

app.get('/mainpage', function(req, res){            
//This is the get method which will Route to the mainpage

// The below line requesting data from mysql database
connection.query('SELECT * FROM tablename', function(err, rows){
    console.log("data send to frontpage");
    res.send(rows); //It will send response to corresponding http method in angular js

});

Post Method :

app.post('/mainpage', function(req, res){           

    // The below line requesting data from mysql database
connection.query('SELECT * FROM tablename', function(err, rows){
console.log("data send to frontpage");
res.send(rows); //It will send response to corresponding http method in angular js

});

Put Method :

//This will insert the data
app.put('/mainpage', function(req, res){           


// The below line inserting data from mysql database
connection.query('insert into tablename value ('name')', function(err, rows){


});

Delete Method :

//This will delete the data in the resource
app.delete('/mainpage', function(req, res){  

//This is the delete method which will Route to the mainpage

// The below line deleting data from mysql database
connection.query('Delete from tablename', function(err, rows){

});
Res.send() :

This Res.send function is used to send the data to client side http method of Angular Js. For Example:

app.post('/mainpage', function(req, res){           
    res.send('Hello world'); //It will send response to corresponding http method in angular js
});

This res.send(‘Hello world’) will send the hello world text to the client side http method of the angular js. Query: Send the data from client side to server side by using query parameter. For Example: Clent side request

app.controller('mainctl',function($scope,$http,$routeParams){
    $http({
        method: 'GET',
        url:'http://localhost:2901/mainpage',
        params: {                               //query parameter send data with the help of params
            urname(this is key):$routeParams.uid(this is value)     //params data will attach as query like http://localhost:2901/mainpage?urname='data'
        }
    }).then (function(res){
        console.log(res.data);
    })
})

Server side receiving request and response to client.

app.get('/mainpage', function(req, res){            // request received from client side
    console.log("using Query")
    console.log(req.query);                         //to access the query parameter use(req.query or req.query.urname(use key))
    reg.find(req.query,function(err,data){          //request sent to mongodb and result will be stored in data parameter
        res.send(data);                             //response will send to client request.
    });
});

Params: Send the data from client side to server side by using Params. For Example: Client Side Request:

app.controller('mainctl',function($scope,$http,$routeParams){
$http({
    method: 'GET',
    url:'http://localhost:2901/mainpage/'+$routeParams.uid(this is value)   // value will send to server through url

    }).then (function(res){
        console.log(res.data);
    })
})

Server side receiving request and response to client.

app.get('/mainpage/:urname', function(req, res){            // request received from client side
    console.log("using Param")
    console.log(req.params);            //to access the query parameter use(req.params or req.params.urname)
    reg.find(req.query,function(err,data){      //request sent to mongodb and result will be stored in data parameter
        res.send(data);             //response will send to client request.
    });
});

Body: Body is used to send data securely(i.e., hide information and send a request to server). Important things to follow while using body parameter. install body-parse.use(bodyParser.json()) - after using this line only body will accept body request from client. For Example: Client side:

app.controller('signinctrl',function($scope,$http,$location){     
    $scope.send data=function(user){                 //user input all store in json type
    $http({
            method: 'POST',
            url: 'http://localhost:2901/signin',
            data:user                               // data information pass through body parameter
        }).then(function(res) {            
            console.log(res.data);
        })  
    }
})

Server Side:

var express = require('express');                   //using express package
var bodyParser = require('body-parser');            // using body-parser package without this data will not work in body parameters
var mongoose = require('mongoose');                 // using mongoose package
var app = express();
app.use(bodyParser.json());                         //access for json files
app.post('/signin',function(req,res){
    console.log("Using Body Method")
    console.log(req.body);                          // access body information using req.body
    reg.find(req.body,function(err,data){           //request result will store in data parameter
        res.send(data);                             // response will send to client resquest
    });
})

Header: Header is used give access control for client side request and response. For Example:

app.all('*',function(req,res,next){                                     //* represent access control to all app using methods.
    console.log("Allow header access");
    res.header('Access-Control-Allow-Origin', '*');                     //origin access control
    res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');  //method access control
    res.header('Access-Control-Allow-Headers', 'Content-Type');         // header access control (json, text/plain,..)
    next();
})

For any query contact Clofus Innovations.

Contact Us

Just leave your email and our support team will help you